home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StateEdit.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  188 lines

  1. /*
  2.  * @(#)StateEdit.java       1.8 97/07/28
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.undo;
  22.  
  23. import java.util.Enumeration;
  24. import java.util.Hashtable;
  25. import java.util.Vector;
  26.  
  27.  
  28. /**
  29. * <P>StateEdit is a general edit for objects that change state.
  30. * Objects being edited must conform to the StateEditable interface.</P>
  31. * <P>This edit class works by asking an object to store it's state in
  32. * Hashtables before and after editing occurs.  Upon undo or redo the
  33. * object is told to restore it's state from these Hashtables.</P>
  34. * A state edit is used as follows:
  35. * <PRE>
  36. *    // Create the edit during the "before" state of the object
  37. *    StateEdit newEdit = new StateEdit(myObject);
  38. *    // Modify the object
  39. *    myObject.someStateModifyingMethod();
  40. *    // "end" the edit when you are done modifying the object
  41. *    newEdit.end();
  42. * </PRE>
  43. * <P><EM>Note that when a StateEdit ends, it removes redundant state from
  44. * the Hashtables - A state Hashtable is not guaranteed to contain all
  45. * keys/values placed into it when the state is stored!</EM></P>
  46. *
  47. * @see StateEditable
  48. */
  49.  
  50. public class StateEdit
  51.     extends AbstractUndoableEdit {
  52.  
  53.     protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $";
  54.  
  55.     //
  56.     // Attributes
  57.     //
  58.  
  59.     /**
  60.      * The object being edited
  61.      */
  62.     protected StateEditable object;
  63.  
  64.     /**
  65.      * The state information prior to the edit
  66.      */
  67.     protected Hashtable preState;
  68.  
  69.     /**
  70.      * The state information after the edit
  71.      */
  72.     protected Hashtable postState;
  73.  
  74.     /**
  75.      * The undo/redo presentation name
  76.      */
  77.     protected String undoRedoName;
  78.  
  79.     //
  80.     // Constructors
  81.     //
  82.     
  83.     /**
  84.      * Create and return a new StateEdit.
  85.      *
  86.      * @param anObject The object to watch for changing state
  87.      *
  88.      * @see StateEdit
  89.      */
  90.     public StateEdit(StateEditable anObject) {
  91.         super();
  92.     init (anObject,null);
  93.     }
  94.  
  95.     /**
  96.      * Create and return a new StateEdit with a presentation name.
  97.      *
  98.      * @param anObject The object to watch for changing state
  99.      * @param name The presentation name to be used for this edit
  100.      *
  101.      * @see StateEdit
  102.      */
  103.     public StateEdit(StateEditable anObject, String name) {
  104.     super();
  105.     init (anObject,name);
  106.     }
  107.  
  108.     protected void init (StateEditable anObject, String name) {
  109.     this.object = anObject;
  110.     this.preState = new Hashtable(11);
  111.     this.object.storeState(this.preState);
  112.     this.postState = null;
  113.     this.undoRedoName = name;
  114.     }
  115.  
  116.  
  117.     //
  118.     // Operation
  119.     //
  120.  
  121.  
  122.     /**
  123.      * Gets the post-edit state of the StateEditable object and
  124.      * ends the edit.
  125.      */
  126.     public void end() {
  127.     this.postState = new Hashtable(11);
  128.     this.object.storeState(this.postState);
  129.     this.removeRedundantState();
  130.     }
  131.  
  132.     /**
  133.      * Tells the edited object to apply the state prior to the edit
  134.      */
  135.     public void undo() {
  136.     super.undo();
  137.     this.object.restoreState(preState);
  138.     }
  139.  
  140.     /**
  141.      * Tells the edited object to apply the state after the edit
  142.      */
  143.     public void redo() {
  144.     super.redo();
  145.     this.object.restoreState(postState);
  146.     }
  147.  
  148.     /**
  149.      * Gets the presentation name for this edit
  150.      */
  151.     public String getPresentationName() {
  152.     return this.undoRedoName;
  153.     }
  154.  
  155.  
  156.     //
  157.     // Internal support
  158.     //
  159.  
  160.     /**
  161.      * Remove redundant key/values in state hashtables.
  162.      */
  163.     protected void removeRedundantState() {
  164.     Vector uselessKeys = new Vector();
  165.     Enumeration myKeys = preState.keys();
  166.  
  167.     // Locate redundant state
  168.     while (myKeys.hasMoreElements()) {
  169.         Object myKey = myKeys.nextElement();
  170.         if (postState.containsKey(myKey) &&
  171.         postState.get(myKey).equals(preState.get(myKey))) {
  172.         uselessKeys.addElement(myKey);
  173.         }
  174.     }
  175.  
  176.     // Remove redundant state
  177.     for (int i = uselessKeys.size()-1; i >= 0; i--) {
  178.         Object myKey = uselessKeys.elementAt(i);
  179.         preState.remove(myKey);
  180.         postState.remove(myKey);
  181.     }
  182.     }
  183.  
  184. } // End of class StateEdit
  185.